package com.starbucks.opencase.websocket;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.function.funSession;
import com.starbucks.opencase.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class wsMessage {
    @Autowired
    private funSession funSession;
    @Autowired
    private serUserAccount serUserAccount;
    @Autowired
    private serUserBag serUserBag;
    @Autowired
    private serUserScan serUserScan;
    @Autowired
    private serUserBattle serUserBattle;
    @Autowired
    private serUserWallet serUserWallet;
    @Autowired
    private serUserOnHook serUserOnHook;
    @Autowired
    private serUserRecharge serUserRecharge;
    @Autowired
    private serUserFriend serUserFriend;
    /**
     * 处理消息
     * @param id
     * @param message
     */
    public void handle(String id, JsonNode message){
        System.out.println("["+ String.format("%29s",LocalDateTime.now().toString()).replaceAll(" ","0") +"]["+id+"][client]\t"+message.toString());
        if(!message.has(enumCMKey.mark.name()))sendError(id, enumCMTip.no_has_mark.getTip());
        else{
            int code=message.get(enumCMKey.mark.name()).asInt();
            if(code== enumCMMark.login.ordinal())serUserAccount.login(id,message);
            else if(code== enumCMMark.sendVerificationCode.ordinal())serUserAccount.sendVerificationCode(id, message);
            else if(code==enumCMMark.updateUserInfo.ordinal())serUserAccount.updateUserInfo(id,message);
            else if(code==enumCMMark.queryActivePet.ordinal())serUserBag.queryActivePet(id,message);
            else if(code==enumCMMark.queryBag.ordinal())serUserBag.queryBag(id,message);
            else if(code==enumCMMark.scan.ordinal())serUserScan.scan(id,message);
            else if(code==enumCMMark.carry.ordinal())serUserScan.carry(id,message);
            else if(code==enumCMMark.queryPetCarryGoods.ordinal())serUserScan.queryCarryGoods(id,message);
            else if(code==enumCMMark.carryDraw.ordinal())serUserScan.carryDraw(id,message);
            else if(code==enumCMMark.battleMatching.ordinal())serUserBattle.matching(id,message);
            else if(code==enumCMMark.battleEnd.ordinal())serUserBattle.end(id,message);
            else if(code==enumCMMark.expendEnergy.ordinal())serUserWallet.expendEnergy(id,message);
            else if(code==enumCMMark.addOnHook.ordinal())serUserOnHook.addOnHook(id,message);
            else if(code==enumCMMark.removeOnHook.ordinal())serUserOnHook.removeOnHook(id,message);
            else if(code==enumCMMark.rechargeMoney.ordinal())serUserRecharge.recharge(id,message);
            else if(code==enumCMMark.changeIdentity.ordinal())serUserAccount.changeIdentity(id,message);
            else if(code==enumCMMark.friendList.ordinal())serUserFriend.list(id,message);
            else if(code==enumCMMark.friendApply.ordinal())serUserFriend.apply(id,message);
            else if(code==enumCMMark.friendApplyActive.ordinal())serUserFriend.applyActive(id,message);
            else if(code==enumCMMark.friendApplyTo.ordinal())serUserFriend.applyTo(id,message);
            else if(code==enumCMMark.friendApplyFrom.ordinal())serUserFriend.applyFrom(id,message);
            else if(code==enumCMMark.friendSend.ordinal())serUserFriend.send(id,message);
            else if(code==enumCMMark.exitLogin.ordinal())serUserAccount.exitLogin(id,message);
            else sendError(id, enumCMTip.no_find_mark.getTip());
        }
    }

    /**
     * 发送错误信息
     * @param id
     * @param message
     */
    private void sendError(String id,String message){
        ObjectMapper om=new ObjectMapper();
        ObjectNode on=om.createObjectNode();
        on.put(enumCMKey.mark.name(), enumCMMark.error.ordinal());
        on.put(enumCMKey.tip.name(), message);
        on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
        funSession.sendMessage(id,on);
    }
}
